using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.IO;
using System.Reflection;

namespace Pavilion
{
    public delegate void RequestSessionLoadHandler(object requester, string sessionName);

    public class GameSession
    {
        private List<GameItem> gameItemList;
        private bool isEnabled;
        public string Name { get; private set; }

        public GameSession(string name)
        {
            this.Name = name;

            gameItemList = new List<GameItem>();
            isEnabled = true;
        }

        public void Load()
        {
            Unload();

            IEnumerable<string> gameItemNameList = GameEngine.GetGameItems(Name);

            foreach (string gameItemName in gameItemNameList)
                gameItemList.Add(new GameItem(gameItemName, Name));

            foreach (GameItem gameItem in gameItemList)
                gameItem.Load();
        }

        public void Unload()
        {
            foreach (GameItem gameItem in gameItemList)
                gameItem.Dipose();

            gameItemList.Clear();
        }

        public void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            foreach (GameItem gameItem in gameItemList)
                gameItem.Update(gameTime);

            CollisionManager.Instance.Update();
        }

        public bool IsEnabled
        {
            get { return isEnabled; }
            set
            {
                isEnabled = value;

                foreach (GameItem gameItem in gameItemList)
                    gameItem.IsEnabled = value;
            }
        }

        public GameItem[] GetGameItems { get { return gameItemList.ToArray(); } }

        static public event RequestSessionLoadHandler SessionLoadRequested;
        static public void RequestSessionLoad(object requester, string sessionName)
        {
            if (SessionLoadRequested != null)
                SessionLoadRequested(requester, sessionName);
        }
    }
}
